#include "app_buffer.h"
sub_buffer_t *App_Buffer_Sub_Buffer_Init(int capacity);

buffer_t *App_Buffer_Init(int capacity)
{
    // 分配缓冲区结构体内存
    buffer_t *buffer = (buffer_t *)malloc(sizeof(buffer_t));
    if (buffer == NULL)
    {
        // 分配内存空间失败
        log_error("分配缓冲区结构体内存失败");
        return NULL;
    }
    // 初始化子缓冲区
    buffer->buffers[0] = App_Buffer_Sub_Buffer_Init(capacity);
    if (buffer->buffers[0] == NULL)
    {
        // 分配内存空间失败
        log_error("分配子缓冲区内存失败");
        free(buffer);
        return NULL;
    }
    buffer->buffers[1] = App_Buffer_Sub_Buffer_Init(capacity);
    if (buffer->buffers[1] == NULL)
    {
        // 分配内存空间失败
        log_error("分配子缓冲区内存失败");
        free(buffer);
        return NULL;
    }
    // 默认第一个缓冲区读数据
    buffer->read_index = 0;
    // 默认第二个缓冲区写数据
    buffer->write_index = 1;

    // 初始化互斥锁
    pthread_mutex_init(&buffer->read_mutex, NULL);
    pthread_mutex_init(&buffer->write_mutex, NULL);

    return buffer;
}

sub_buffer_t *App_Buffer_Sub_Buffer_Init(int capacity)
{
    // 分配内存空间
    sub_buffer_t *sub_buffer = (sub_buffer_t *)malloc(sizeof(sub_buffer_t));
    if (sub_buffer == NULL)
    {
        // 分配内存空间失败
        log_error("分配子缓冲区结构体内存失败");
        return NULL;
    }
    // 分配缓冲区内存
    sub_buffer->ptr = (char *)malloc(capacity);
    if (sub_buffer->ptr == NULL)
    {
        // 分配内存空间失败
        log_error("分配子缓冲区内存失败");
        free(sub_buffer);
        return NULL;
    }
    memset(sub_buffer->ptr, 0, capacity);
    // 初始化长度
    sub_buffer->len = 0;
    // 初始化容量
    sub_buffer->capacity = capacity;
    return sub_buffer;
}
// 向写缓冲区中写入数据
GateState_t App_Buffer_Write(buffer_t *buffer, char *data, char len)
{
    // 添加写锁
    pthread_mutex_lock(&buffer->write_mutex);
    // 找到要写入的缓冲区
    sub_buffer_t *sub_buffer = buffer->buffers[buffer->write_index];
    // 判断空间是否足够
    if ((sub_buffer->capacity) < (sub_buffer->len + 1))
    {
        // 容量不足
        log_error("容量不足,剩余的容量为：%d,要写入的容量为：%d", sub_buffer->capacity, sub_buffer->len + 1);
        // 解锁
        pthread_mutex_unlock(&buffer->write_mutex);
        return GATE_ERROR;
    }
    // 写入数据
    // 写入数据的长度
    sub_buffer->ptr[sub_buffer->len] = len;
    // 写入数据
    memcpy(sub_buffer->ptr + sub_buffer->len + 1, data, len);
    // 更新长度
    sub_buffer->len += (len + 1);
    // 解锁
    pthread_mutex_unlock(&buffer->write_mutex);
    log_info("缓冲区写入数据成功");
}
/**
 * @brief 读取缓冲区数据
 *
 * @param buffer 缓冲区指针
 * @param data 读取到的数据
 * @param size 能够读取的数据最大长度
 * @return int 读取到的数据实际长度
 */
int App_Buffer_Read(buffer_t *buffer, char data[], int size)
{
    // 添加读锁
    pthread_mutex_lock(&buffer->read_mutex);
    // 找到要读的缓冲区
    sub_buffer_t *sub_buffer = buffer->buffers[buffer->read_index];
    // 如果缓冲区为空则需要交换缓冲区
    if (sub_buffer->len == 0)
    {
        // 交换缓冲区
        pthread_mutex_lock(&buffer->write_mutex);
        int temp = buffer->read_index;
        buffer->read_index = buffer->write_index;
        buffer->write_index = temp;
        pthread_mutex_unlock(&buffer->write_mutex);
        sub_buffer = buffer->buffers[buffer->read_index];
        if (sub_buffer->len == 0)
        {
            //log_info("缓冲区为空");
            pthread_mutex_unlock(&buffer->read_mutex);
            return 0;
        }
    }
    // 先读取长度
    char len = sub_buffer->ptr[0];
    if (len > size)
    {
        log_info("超出可读取的容量");
        pthread_mutex_unlock(&buffer->read_mutex);
        return 0;
    }
    // 读取数据
    memcpy(data, sub_buffer->ptr + 1, len);
    // 将剩余的数据移动到最前面
    memmove(sub_buffer->ptr, sub_buffer->ptr + 1 + len, sub_buffer->len - 1 - len);

    // 跟新长度
    sub_buffer->len -= (len + 1);
    // 解锁
    pthread_mutex_unlock(&buffer->read_mutex);
    return len;
}

void App_Buffer_Deinit(buffer_t *buffer)
{
    // 释放空间
    free(buffer->buffers[0]);
    free(buffer->buffers[0]->ptr);
    free(buffer->buffers[1]);
    free(buffer->buffers[1]->ptr);
    // 销毁锁
    pthread_mutex_destroy(&buffer->read_mutex);
    pthread_mutex_destroy(&buffer->write_mutex);

    free(buffer);
}
